﻿var XmlNodeManage = {};

XmlNodeManage.GetDocumentNode = function(taskGuid, dataGuid, dataType) {
    var xml = document.createElement("xml");
    xml.loadXML("<Document TaskGuid=\"\" DataGuid=\"\" DataType=\"\" />");

    if (taskGuid != undefined)
        this.SetSubValue(xml.documentElement, "@TaskGuid", null, taskGuid);
    if (dataGuid != undefined)
        this.SetSubValue(xml.documentElement, "@DataGuid", null, dataGuid);
    if (dataType != undefined)
        this.SetSubValue(xml.documentElement, "@DataType", null, dataType);
    return xml.documentElement;
};

XmlNodeManage.CreateNode = function (parentNode, tagName) {
    if (parentNode == null)
        return null;

    if (tagName.charAt(0) == '@')
        return parentNode.ownerDocument.createAttribute(tagName.substr(1));

    return parentNode.ownerDocument.createElement(tagName);
};

XmlNodeManage.AddSubValue = function(parentNode, tagName, typeName, value) {
    if (parentNode == null)
        return null;

    var dataNode = this.CreateNode(parentNode, tagName);
    parentNode.appendChild(dataNode);
    dataNode.text = value;

    if (typeName != null)
        this.SetSubValue(dataNode, "@Type", null, typeName);
    return dataNode;
};

XmlNodeManage.SetSubValue = function(parentNode, tagName, typeName, value) {
    if (parentNode == null)
        return null;

    if (tagName.charAt(0) == '@') {
        tagName = tagName.substr(1);

        var attribute = parentNode.attributes.getNamedItem(tagName);
        if (attribute == null) {
            attribute = parentNode.ownerDocument.createAttribute(tagName);
            parentNode.attributes.setNamedItem(attribute);
        }
        attribute.value = value;
    }
    else {
        var dataNode = parentNode.selectSingleNode(tagName);
        if (dataNode == null) {
            if (tagName.charAt(0) == '/') {
                return null;
            }
            dataNode = parentNode.ownerDocument.createElement(tagName);
            parentNode.appendChild(dataNode);
        }
        if (typeName != null)
            this.SetSubValue(dataNode, "@Type", null, typeName);
        dataNode.text = value;
    }
    return dataNode;
};

XmlNodeManage.GetSubValue = function (node, tagName) {
    if (node == null)
        return null;

    if (tagName == "@")
        return node.text;

    var retValue;
    if (tagName.charAt(0) == '@') {
        tagName = tagName.substr(1);
        var attribute = node.attributes.getNamedItem(tagName);
        if (attribute == null)
            retValue = "";
        else
            retValue = attribute.value;
    }
    else {
        var dataNode = node.selectSingleNode(tagName);
        if (dataNode == null)
            retValue = "";
        else
            retValue = dataNode.text;
    }
    return retValue;
};

XmlNodeManage.GetSubNode = function(node) {
    if (node == null)
        return null;

    if (node.childNodes.length <= 0)
        return null;
    return node.childNodes.item(0);
};

function XslFile(url) {
    this.xslFile = document.createElement("xml");
    this.xslFile.async = false;
    this.xslFile.load(url);
};

XslFile.prototype.TransformNode = function (xmlData) {
    return xmlData.transformNode(this.xslFile);
};

Array.prototype.indexOf = function (object) {
    for (var nCount = 0; nCount < this.length; nCount++) {
        if (this[nCount] === object)
            return nCount;
    }
    return -1;
};

Array.prototype.lastIndexOf = function (object) {
    var nCount = this.length;
    while (nCount > 0) {
        nCount--;
        if (this[nCount] === object)
            return nCount;
    }
    return -1;
};

function TraversalSubNode(XmlData, TraversalFunction) {
    if (XmlData == null)
        return;

    var subData;

    for (var i = 0; i < XmlData.childNodes.length; i++) {
        subData = XmlData.childNodes[i];
        TraversalFunction(subData);
    }
};
